/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.*;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoBusqueda;

/**
 *
 * @author Miguel Romero
 */
public class ClienteListar extends javax.swing.JFrame {

    private final ClienteLnLocal clienteControl;
    private DefaultTableModel clientemodelo;
    private List<Cliente> clientelista;
    private JFrame clienteVentana;
    /**
     * Creates new form ProductoListar
     */
    public ClienteListar(ClienteLnLocal clienteControl) {
        this.clienteControl = clienteControl;
        initComponents();
        String nombres[] = {"Nombre","Sexo","Id","Telefono","Direccion"};
        
        clientemodelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEditable(int row, int column){
            return false;
            }
        };
        jTable1.setModel(clientemodelo);

        clientelista=clienteControl.cliengetTodos();
        for (Cliente c : clientelista) {
            Object clienterenglon []=new Object[5];
            clienterenglon[0] = c.getNombre();
            clienterenglon[1] = c.getSexo();
            clienterenglon[2] = c.getId()+"";
            clienterenglon[3] = c.getTelefono()+"";
            clienterenglon[4] = c.getDireccion();
            clientemodelo.addRow(clienterenglon);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 500, 250));

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Contiene");
        getContentPane().add(jRadioButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Inicia");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jRadioButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, 260, 50));
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 530, 330));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        List<Cliente> ctmp= new ArrayList<>();
        if(jRadioButton1.isSelected()){
           ctmp= clienteControl.cliengetPorNombre(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda.CONTIENE);  
        }else{
            ctmp= clienteControl.cliengetPorNombre(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda.INICIA);  
        }
        String nombres[] = {"Codigo", "Nombre", "Precio", "Departamento", "Descripcion"};
        clientemodelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEditable(int row, int column){
            return false;
            }
        };
        for (Cliente c : ctmp) {
            String renglon[] = new String[5];
            renglon[0] = c.getNombre();
            renglon[1] = c.getSexo();
            renglon[2] = c.getId() + "";
            renglon[3] = c.getTelefono()+"";
            renglon[4] = c.getDireccion();
            clientemodelo.addRow(renglon);
        }
        jTable1.setModel(clientemodelo);
        clientelista=ctmp;
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1KeyTyped

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        if(evt.getClickCount()==2){
            System.out.println(jTable1.getSelectedRow());
            Cliente c = clientelista.get(jTable1.getSelectedRow());
            if(clienteVentana==null){
                clienteVentana=new DialogoCliente(c,clienteControl);
            }else{
                clienteVentana.dispose();
                clienteVentana=new DialogoCliente(c,clienteControl);
                }
            clienteVentana.setVisible(true);
        }
    }//GEN-LAST:event_jTable1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
